package com.module.admin.log.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.log.dao.BizLogDao;
import com.module.admin.log.pojo.BizLog;
import com.module.admin.log.pojo.BizLogDtl;
import com.module.admin.log.service.BizLogDtlService;
import com.module.admin.log.service.BizLogService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseFrame;
import com.system.handle.model.ResponseCode;

/**
 * biz_log的Service
 * @author admin
 * @date 2019-11-29 10:57:13
 * @version V1.0.0
 */
@Component
public class BizLogServiceImpl implements BizLogService {

	private static final Logger LOGGER = LoggerFactory.getLogger(BizLogServiceImpl.class);
	@Autowired
	private BizLogDao bizLogDao;
	@Autowired
	private BizLogDtlService bizLogDtlService;
	
	/**
	 * 保存
	 * @param bizLog
	 * @param dtlString
	 * @return
	 */
	@Override
	public ResponseFrame save(BizLog bizLog, String dtlString) {
		ResponseFrame frame = new ResponseFrame();
		if (FrameStringUtil.isEmpty(dtlString)) {
			frame.setCode(-2);
			frame.setMessage("详情不能为空");
			return frame;
		}
		List<BizLogDtl> dtls = FrameJsonUtil.toList(dtlString, BizLogDtl.class);
		if (dtls == null || dtls.size() == 0) {
			frame.setCode(-2);
			frame.setMessage("详情不能为空");
			return frame;
		}
		BizLog org = null;
		if (FrameStringUtil.isEmpty(bizLog.getTraceId())) {
			bizLog.setTraceId(FrameNoUtil.uuid());
		} else {
			org = get(bizLog.getTraceId());
		}
		if (org == null) {
			bizLog.setReqTime(FrameTimeUtil.getTime());
			try {
				bizLogDao.save(bizLog);
			} catch (Exception e) {
				LOGGER.error("保存记录异常: " + e.getMessage());
			}
		}
		
		for (BizLogDtl bld : dtls) {
			bld.setTraceId(bizLog.getTraceId());
			bizLogDtlService.save(bld);
		}
		
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	/**
	 * 根据traceId获取对象
	 * @param traceId
	 * @return
	 */
	@Override
	public BizLog get(String traceId) {
		return bizLogDao.get(traceId);
	}

	/**
	 * 分页获取对象
	 * @param bizLog
	 * @return
	 */
	@Override
	public ResponseFrame pageQuery(BizLog bizLog) {
		bizLog.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = bizLogDao.findBizLogCount(bizLog);
		List<BizLog> data = null;
		if(total > 0) {
			data = bizLogDao.findBizLog(bizLog);
		}
		Page<BizLog> page = new Page<BizLog>(bizLog.getPage(), bizLog.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	/**
	 * 根据traceId删除对象
	 * @param traceId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String traceId) {
		ResponseFrame frame = new ResponseFrame();
		bizLogDao.delete(traceId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public BizLog getDtl(String traceId) {
		BizLog bl = get(traceId);
		if (bl != null) {
			List<BizLogDtl> dtls = bizLogDtlService.findByTraceId(traceId);
			bl.setDtls(dtls);
			List<String> spanIds = new ArrayList<String>();
			for (BizLogDtl bizLogDtl : dtls) {
				if (!spanIds.contains(bizLogDtl.getSpanId())) {
					spanIds.add(bizLogDtl.getSpanId());
				}
			}
			bl.setSpanIds(spanIds);
		}
		return bl;
	}
}